/**
* @program: src
*
* @description:登录注册接口
*
* @author: Mr.chen
*
* @create: 2020-11-04 17:58
**/
package login
import (
	"ydd-im/service/api/services/wsservice/account"
	"ydd-im/service/api/tools/bind"
	"ydd-im/service/api/tools/response"
	//"fmt"
	"github.com/gin-gonic/gin"
	//"github.com/go-playground/validator/v10"
)

type Controller struct {
}

type inputData struct {
	Username string `json:"username" form:"username" binding:"required" `
	Passwd  string `json:"password" form:"password" binding:"required" `
	Userid  string `json:"userid" form:"userid" `
}
type inputLoginData struct {
	Username string `json:"username" form:"username" binding:"required" `
	Passwd  string `json:"password" form:"password" binding:"required" `
	Client  string `json:"client" form:"client" binding:"required" `
}
/**
 * 注册账号
 * @access  public
 * @param   string     username  账号
 * @param   string     passwd  密码
 * @param   string     userkey  用户id
 * @param   string     appkey  appkey
 * @return  mix
 */
func (r *Controller) Register(c *gin.Context) {
	var inputData inputData
	_, err := bind.Bind(&inputData, c)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:  response.ErrorCode,
			Message: "参数有误，请重试",
		})
		return
	}
	//// 参数验证
	//validate := validator.New()
	//// 注册自定义验证
	////_ = validate.RegisterValidation("NameValid", param_verify.NameValid)
	//if err = validate.Struct(s); err != nil {
	//	response.ResponseJsonFail(c, response.Response{
	//		Code:  response.ErrorCode,
	//		Message: "参数有误，请重试",
	//	})
	//	return
	//}

	appkey := c.Request.FormValue("appkey")
	err = account.RegisterIsClusterUser(inputData.Username,inputData.Passwd,inputData.Userid,appkey)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:  response.ErrorCode,
			Message: err.Error(),
		})
		return
	}
	response.ResponseJsonOk(c, response.Response{
		Code:  response.SuccessCode,
		Message: "success",
	})
	return
}
/**
 * 登录
 * @access  public
 * @param   string     username  账号
 * @param   string     passwd  密码
 * @param   string     client  客户端
 * @param   string     appkey  appkey
 * @return  mix        data
 */
func (r *Controller) Login(c *gin.Context) {
	var inputLoginData inputLoginData
	_, err := bind.Bind(&inputLoginData, c)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:  response.ErrorCode,
			Message: "参数有误，请重试",
		})
		return
	}
	appkey := c.Request.FormValue("appkey")
	resp,err := account.LoginIsClusterUser(c,inputLoginData.Username,inputLoginData.Passwd,inputLoginData.Client,appkey)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:  response.ErrorCode,
			Message: err.Error(),
		})
		return
	}
	response.ResponseJsonOk(c, response.Response{
		Code:  response.SuccessCode,
		Message: "success",
		Data:resp,
	})
	return
	//user, err := userService.Login(userParam.Mobile, userParam.Passwd)
	//if err != nil {
	//	util.RespFail(c, model.ApiResp{
	//		ErrorNo:  http.StatusInternalServerError,
	//		ErrorMsg: err.Error(),
	//	}, err)
	//	return
	//}
	//
	//util.RespOK(c, model.ApiResp{
	//	ErrorNo:  util.SuccessCode,
	//	ErrorMsg: "",
	//	Data:     user,
	//})

}
